/**
 * RMB银行其它收入/其它支出明细管理初始化
 */
var RmbCashReceipts = {
    id: "RmbCashReceiptsTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
RmbCashReceipts.initColumn = function () {
    return [
            {field: 'selectItem', radio: true},
            {title: '日期', field: 'revdate', visible: true, align: 'center', valign: 'middle',
			formatter:function(value,row,index){
                        return value.substring(0,10);
				}
			},
            {title: '摘要', field: 'note', visible: true, align: 'center', valign: 'middle'},
			{title: '往来客户', field: 'customerName', visible: true, align: 'center', valign: 'middle'},
            {title: '收支类型', field: 'typeName', visible: true, align: 'center', valign: 'middle'},
            {title: '收支明细', field: 'summaryName', visible: true, align: 'center', valign: 'middle'},
            {title: '收款金额', field: 'amount', visible: true,halign:'center', align: 'right', valign: 'middle',
                formatter:function(value,row,index){ if(row.type==1){return value;}else{ return '';}}},
            {title: '支出金额', field: 'amount', visible: true,halign:'center', align: 'right', valign: 'middle',
                formatter:function(value,row,index){ if(row.type==2){return value;}else{ return '';}}},


    ];
};

/**
 * 检查是否选中
 */
RmbCashReceipts.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        RmbCashReceipts.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加RMB银行其它收入/其它支出明细
 */
RmbCashReceipts.openAddRmbCashReceipts = function () {
    var index = layer.open({
        type: 2,
        title: '添加RMB银行其它收入/其它支出明细',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/rmbCashReceipts/rmbCashReceipts_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看RMB银行其它收入/其它支出明细详情
 */
RmbCashReceipts.openRmbCashReceiptsDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: 'RMB银行其它收入/其它支出明细详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/rmbCashReceipts/rmbCashReceipts_update/' + RmbCashReceipts.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除RMB银行其它收入/其它支出明细
 */
RmbCashReceipts.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/rmbCashReceipts/delete", function (data) {
            Feng.success("删除成功!");
            RmbCashReceipts.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("rmbCashReceiptsId",this.seItem.id);
        ajax.start();
    }
};

RmbCashReceipts.resetSearch = function () {
    $("#platform").val("");
    $("#beginDate").val("");
    $("#endDate").val("");
    $("#type").val("");
}

RmbCashReceipts.formParams = function() {
    var queryData = {};
    queryData['platform'] = $("#platform").val();
    queryData['beginDate'] = $("#beginDate").val();
    queryData['endDate'] = $("#endDate").val();
    queryData['type'] = $("#type").val();
    return queryData;
}

/**
 * 查询RMB银行其它收入/其它支出明细列表
 */
RmbCashReceipts.search = function () {

    RmbCashReceipts.table.refresh({query: RmbCashReceipts.formParams()});
    $("#RmbCashReceiptsSum").remove();
    $("#RmbCashReceiptsTable").after(  RmbCashReceipts.tatol()   )
};

RmbCashReceipts.tatol= function(){

    var tatol;
    var ajax = new $ax(Feng.ctxPath + "/rmbCashReceipts/total", function (result) {
        tatol=result;
    }, function (result) {
        Feng.error("查询失败!" + result.responseJSON.message + "!");
    });
    ajax.setData(RmbCashReceipts.formParams());
    ajax.start();
    var rowTem = '<table id="RmbCashReceiptsSum" data-mobile-responsive="true" data-click-to-select="true" class="table table-hover table-striped" style="margin-top: -0px;color: #2A2E36;font-size: 14px;"><tbody id="RmbCashReceiptsSum" >'+
        '<tr data-index="0">'+
		'<td class="bs-checkbox  " style="width: 35px;"></td>'+
        '<td style="text-align: center; vertical-align: middle; "></td>'+
        '<td style="text-align: right; vertical-align: middle; "></td>'+
        '<td style="text-align: right; vertical-align: middle;"></td>'+
        '<td style="text-align: right; vertical-align: middle;"></td>'+
		'<td style="text-align: center; vertical-align: middle; ">总计：</td> '+
        '<td style="text-align: right; vertical-align: middle; ">'+tatol.income+'</td>'+
        '<td style="text-align: right; vertical-align: middle;">'+tatol.payout+'</td> '+
        '</tr></tbody></table>';
	
    return rowTem;
}

RmbCashReceipts.checkPayTime = function(){
    var beginDate =  $("#beginDate").val();
    var endDate =  $("#endDate").val();
    if (beginDate==null||beginDate==""){
        Feng.info("请选择要导出的开始时间");
        return false;
    } else {
        return true;
    }
    if (endDate==null||endDate==""){
        Feng.info("请选择要导出的结束时间");
        return false;
    } else {
        return true;
    }
};

/**
 * 打开导出页面
 */
RmbCashReceipts.operExport = function(){
    if (this.checkPayTime()) {
        window.location.href = "/borrowing/load?beginDate=" + $("#beginDate").val()+"&endDate=" + $("#endDate").val()+"&type=" + $("#type").val();
    }
};

$(function () {
    var defaultColunms = RmbCashReceipts.initColumn();
    var table = new BSTable(RmbCashReceipts.id, "/rmbCashReceipts/list", defaultColunms,RmbCashReceipts);
    table.setPaginationType("server");
    RmbCashReceipts.table = table.init();
    $(".fixed-table-toolbar").after("<div style='text-align: center;vertical-align: middle;font-size: 22px;color: #6979d7;font-weight: bold;'> RMB银行其它收入/其它支出明细表</div>");
    $("#RmbCashReceiptsTable").after(RmbCashReceipts.tatol());
});
